﻿#include "MeasureView/ToolBarZoom.h"
#include "MeasureView/MeasureView.h"
#include <QDebug>
#include <QLabel>
#include <QSpinBox>
#include <QCheckBox>
#include <QtMath>

namespace Qly {

ToolBarZoom::ToolBarZoom(const QString &title, QWidget *parent)
    :QToolBar(title, parent)
{
    setIconSize(QSize(32, 32));
    initContent();
    pMeasureView = nullptr;
}

void ToolBarZoom::setZoomFactor(double f)
{
    int n = qRound(f * 100.0);
    if( n != spinBoxZoom->value() ) //防止死循环
    {
        spinBoxZoom->setValue(n);
    }

}

void ToolBarZoom::on_spinBoxZoomValueChanged(int n)
{
    if(pMeasureView)
    {
        double f = n / 100.0;
        pMeasureView->setZoomFactor(f);
    }
}

void ToolBarZoom::on_checkBoxToggled(bool on)
{
    if(pMeasureView)
    {
        pMeasureView->centerCross().setEnable(on);
    }
}

void ToolBarZoom::on_doubleSpinBoxRotateValueChanged(double v)
{
    if(pMeasureView)
    {
        pMeasureView->centerCross().setRotate(v);
    }
}

void ToolBarZoom::on_spinBoxCrossLineLength(int n)
{
    if(pMeasureView)
    {
        QSize sz = pMeasureView->imageSize();
        int w = sz.width() * n / 200.0;
        int h = sz.height() * n / 200.0;

        pMeasureView->centerCross().setCrossSize(w, h);
    }
}

void ToolBarZoom::loadSettings(QSettings &settings)
{
    int zoom = settings.value("Display/ZoomFactor", 70).toInt();
    double rotate = settings.value("Display/Rotate", 0).toDouble();
    bool cb = settings.value("Display/ShowCross", false).toBool();
    int length = settings.value("Display/CrossLength", 10).toInt();
    spinBoxZoom->setValue(zoom);
    spinBoxRotate->setValue(rotate);
    checkBox->setChecked(cb);
    spinBoxCrossLineLength->setValue(length);
}
void ToolBarZoom::writeSettings(QSettings &settings)
{
    int zoom = spinBoxZoom->value();
    double rotate = spinBoxRotate->value();
    bool cb = checkBox->isChecked();
    int length = spinBoxCrossLineLength->value();

    settings.setValue("Display/ZoomFactor", zoom);
    settings.setValue("Display/Rotate", rotate);
    settings.setValue("Display/ShowCross", cb);
    settings.setValue("Display/CrossLength", length);

}

void ToolBarZoom::attach(MeasureView *widget)
{
    if(widget != pMeasureView)
    {
        disconnect(pMeasureView, &MeasureView::zoomFactorChanged, this, &ToolBarZoom::setZoomFactor);
    }
    pMeasureView = widget;
    if(pMeasureView)
    {
        connect(pMeasureView, &MeasureView::zoomFactorChanged, this, &ToolBarZoom::setZoomFactor);
        spinBoxZoom->setValue(pMeasureView->zoomFactor());
        //checkBox->setChecked(false);
    }
}

void ToolBarZoom::initContent()
{
    setWindowTitle("Image Zoom Toolbar");
    setObjectName(QStringLiteral("ToolBarZoom"));

    QLabel *label = new QLabel(tr("Zoom Factor: "), this);
    addWidget(label);

    spinBoxZoom = new QSpinBox;
    spinBoxZoom->setSuffix(" %");
    spinBoxZoom->setRange(10, 1000);
    spinBoxZoom->setValue(100);
    spinBoxZoom->setSingleStep(1);
    spinBoxZoom->setFixedWidth(80);
    addWidget(spinBoxZoom) ;
    addSeparator();
    checkBox = new QCheckBox(QStringLiteral("+"));
    addWidget(checkBox) ;
    checkBox->setChecked(false);

    spinBoxRotate = new QDoubleSpinBox(this);
    spinBoxRotate->setRange(-90, 90);
    spinBoxRotate->setDecimals(1);
    spinBoxRotate->setSingleStep(0.5);

    addWidget(spinBoxRotate);

    spinBoxCrossLineLength = new QSpinBox(this);
    spinBoxCrossLineLength->setRange(0, 100);
    spinBoxCrossLineLength->setSingleStep(1);
    spinBoxCrossLineLength->setValue(10);

    addWidget(spinBoxCrossLineLength);

    connect(spinBoxZoom, qOverload<int>(&QSpinBox::valueChanged), this, &ToolBarZoom::on_spinBoxZoomValueChanged);
    //connect(spinBoxZoom, SIGNAL(valueChanged(int)), this, SLOT(on_spinBoxZoomValueChanged(int)));
    connect(checkBox, &QCheckBox::toggled, this, &ToolBarZoom::on_checkBoxToggled);
    connect(spinBoxRotate, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &ToolBarZoom::on_doubleSpinBoxRotateValueChanged);
    connect(spinBoxCrossLineLength, qOverload<int>(&QSpinBox::valueChanged), this, &ToolBarZoom::on_spinBoxCrossLineLength);


}

ToolBarZoom::ToolBarZoom(QWidget *parent)
    :QToolBar(parent)
{
    setIconSize(QSize(32, 32));

    initContent();
    pMeasureView = nullptr;
}

} //namespace Qly
